/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Dao;

import com.sgsmcan.Entidad.Perro;
import com.sgsmcan.Entidad.Sereno;
import com.sgsmcan.Entidad.SerenoPerro;
import com.sgsmcan.Hibernate.HibernateUtil;
import com.sgsmcan.Utils.Utilitario;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author David
 */
public class SerenoDaoImpl extends HibernateDao<Sereno> implements SerenoDao<Sereno> {

    @Override
    public DetachedCriteria getSerenos() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Sereno.class, "sereno").add(Restrictions.eq("sereno.estado",Boolean.TRUE));
        return criteria;
    }

    @Override
    public Sereno getSereno(Integer id) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Sereno.class, "sereno").add(Restrictions.eq("sereno.id", id));
            return (Sereno) criteria.uniqueResult();
        } catch (Exception e) {
            Utilitario.LOG.error("Error en SerenoDaoImpl", e);
            throw new HibernateException(e);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    @Override
    public boolean existeDni(String dni, boolean update) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            String hql = "from Sereno as ser where ser.dni = :dato ";
            if (update) {
                hql = hql.concat(" and ser.dni <> :dato");
            }
            Query query = session.createQuery(hql);
            query.setParameter("dato", dni);
            Sereno aux = null;
            if ((aux = (Sereno) query.uniqueResult()) == null) {
                return false;
            }
            return true;
        } catch (Exception e) {
            Utilitario.LOG.error("Error en SerenoDaoImpl", e);
            throw new HibernateException(e);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public List<Sereno> getSerenos2() {
        List<Sereno> list = null;
        Session session = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT DISTINCT SE FROM Sereno SE LEFT JOIN FETCH SE.serenosperros sp where sp.spk.sereno.id IS NULL OR sp.estado = 0 AND SE.estado = 1");
            list = query.list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return list;
    }

    public List<Sereno> getSerenoUpdate() {
        List<Sereno> list = null;
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("FROM Sereno where estado = 1");
            list = query.list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return list;
    }

//    @Override
//    public List<Sereno> autoComplete(String parametro) {
//        List<Sereno> list = null;
//        Session session = null;
//        try {
//            session = HibernateUtil.getSessionFactory().openSession();
//            Query query = session.createQuery("SELECT se FROM Sereno AS se WHERE se.apellido LIKE :parat");
//            query.setParameter("parat", "%"+parametro+"%");
//            query.setMaxResults(10);
//            list = query.list();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (session != null) {
//                session.close();
//            }
//        }
//        return list;        
//    }
    @Override
    public List<String> autoComplete(String parametro) {
        List<String> list = null;
        Session session = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT CONCAT(se.nombre,' ',se.apellido) as fullname FROM Sereno as se where se.apellido LIKE :parat");
            query.setParameter("parat", "%" + parametro + "%");
            query.setMaxResults(10);
            list = query.list();

        } catch (HibernateException e) {
        } finally {
            if (session != null) {
                session.close();
            }

        }
        return list;
    }

    @Override
    public List<Sereno> autocompletar(String parametro) {
        List<Sereno> list = null;
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT se FROM Sereno AS se WHERE se.apellido LIKE :parat");
            query.setParameter("parat", "%" + parametro + "%");
            query.setMaxResults(10);
            list = query.list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return list;
    }

    @Override
    public Integer getSerenoId(String parametro) {
        Integer idSereno = 0;
        Session sesion = null;
        List<Integer> idSe = null;
        try {
            sesion = HibernateUtil.getSessionFactory().openSession();
            Query query = sesion.createQuery("SELECT se.id FROM Sereno as se WHERE se.nombre LIKE :parat OR se.apellido LIKE :parat");
            query.setParameter("parat", "%" + parametro + "%");
            idSe = query.list();

            idSereno = idSe.get(0);

        } catch (HibernateException e) {
        } finally {
            if (sesion != null) {
                sesion.close();
            }
        }
        return idSereno;
    }

    @Override
    public Sereno getSereno(String parameter) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
